<?php
package("Expression");
package("Expression_Operand");
package("Expression_Operator");
package("Query");
package("Utils");

class Query_SelectOne implements Query_Builder {
	
	/**
	 * @var Expression_Operator_Left
	 */
	private $_select; // SELECT id, email, password, date_added FROM `users` WHERE id = '4'
	
	/**
	 * @var Expression_Operator
	 */
	private $_separator;
	
	/**
	 * @var Expression_Operator_Unary
	 */
	private $_from;
	
	public function __construct() {
		$factory = Expression_Operator_Factory::getSingleton();
		$this->_separator = $factory->construct("separator", array());
		$this->_from = $factory->construct("from", array());
		$this->_select = $factory->construct("select", array($this->_separator, $this->_from));
	}
	
	public function bindTable($table) {
		$table = new Expression_Operand_Table($table);
		$this->_from->addOperand($table);
	}
	
	public function bindKey(Core_Property $bound) {
		$factory = Expression_Operator_Factory::getSingleton();
		$column = new Expression_Operand($bound->column);
		$value = new Expression_Operand_Variable($bound->value);
		$equals = $factory->construct("equals", array($column, $value));
		$where = $factory->construct("where", array($equals));
		$this->_select->addOperand($where);
		$this->bindProperty($bound);
	}
	
	public function bindProperty(Core_Property $bound) {
		$factory = Expression_Operator_Factory::getSingleton();
		$column= new Expression_Operand($bound->column);
		$this->_separator->addOperand($column);
	}
	
	public function toSql() {
		return $this->_select->evaluate();
	}
}
?>